package com.xyazm.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xyazm.common.utils.PageUtils;
import com.xyazm.common.utils.R;
import com.xyazm.product.entity.ProductAttrValueEntity;
import com.xyazm.product.entity.vo.AttrRespVo;
import com.xyazm.product.entity.vo.AttrVo;
import com.xyazm.product.feign.CouponFeignService;
import com.xyazm.product.service.AttrService;
import com.xyazm.product.service.ProductAttrValueService;



/**
 * 商品属性
 *
 * @author xuyou
 * @date 2020-06-11 17:15:45
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
   
    @Autowired
    private AttrService attrService;

    @Autowired
    private CouponFeignService feign;
    
    @Autowired
    private ProductAttrValueService productAttrValueService;
//    @RequestMapping("/feign")
//    public R feign(){
//    	SpuBoundTo spuBoundTo = new SpuBoundTo();
//    	spuBoundTo.setBuyBounds(new BigDecimal(11));
//    	spuBoundTo.setGrowBounds(new BigDecimal(22));
//    	spuBoundTo.setSpuId(1L);
//    	R test = feign.test(spuBoundTo);
//        return test;
//    }
    

    // /product/attr/base/listforspu/{spuId}
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrlistforspu(@PathVariable("spuId") Long spuId){

        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrlistforspu(spuId);

        return R.ok().put("data",entities);
    }
    
    ///product/attr/update/{spuId}
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> entities){

        productAttrValueService.updateSpuAttr(spuId,entities);
        return R.ok();
    }

    /**
     * 获取分类销售属性
     * 返回数据:   {
	"msg": "success",
	"code": 0,
	"page": {
		"totalCount": 0,
		"pageSize": 10,
		"totalPage": 0,
		"currPage": 1,
		"list": [{
			"attrId": 0, //属性id
			"attrName": "string", //属性名
			"attrType": 0, //属性类型，0-销售属性，1-基本属性
			"catelogName": "手机/数码/手机", //所属分类名字
			"groupName": "主体", //所属分组名字
			"enable": 0, //是否启用
			"icon": "string", //图标
			"searchType": 0,//是否需要检索[0-不需要，1-需要]
			"showDesc": 0,//是否展示在介绍上；0-否 1-是
			"valueSelect": "string",//可选值列表[用逗号分隔]
			"valueType": 0//值类型[0-为单个值，1-可以选择多个值]
		}]
	}
}
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType")String type){

        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,type);
        return R.ok().put("page", page);
    }
    
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        
        PageUtils page = attrService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		
//		AttrEntity attr = attrService.getById(attrId);
    	
    	 AttrRespVo respVo = attrService.getAttrInfo(attrId);
         return R.ok().put("attr", respVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		
		attrService.saveAttr(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
		
//		attrService.updateById(attr);
    	attrService.updateAttr(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		
		attrService.removeByIds(Arrays.asList(attrIds));
        return R.ok();
    }

}
